package com.lcm.learn.lcmcode.serializable;

import java.io.*;
import java.util.LinkedList;

/**
 * @description:
 * @author: lcm
 * @create: 2020-01-02 11:54
 **/

public class SerializableTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        test2();
        test3();
        test4();
    }

    public static void test2() throws IOException, ClassNotFoundException {
        Node node1=new Node("1");
        Node node2=new Node("2");
        Node node3=new Node("3");
        Node node4=new Node("4");
        node1.next=node2;
        node2.pre=node1;
        node2.next=node3;
        node3.pre=node2;
        node3.next=node4;
        node4.pre=node3;

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(node1);
        out.close();

        byte[] bytes = bout.toByteArray();
        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(bytes));
        Node node=(Node)in.readObject();
        System.out.println(node);
    }

    public static void test4() throws IOException, ClassNotFoundException {
        LinkedList list=new LinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(list);
        out.close();
        byte[] bytes = bout.toByteArray();
        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(bytes));
        LinkedList list1=(LinkedList)in.readObject();
        System.out.println(list1);
    }
    public static void test3() throws IOException, ClassNotFoundException {
        NodeTransient node1=new NodeTransient("1");
        NodeTransient node2=new NodeTransient("2");
        NodeTransient node3=new NodeTransient("3");
        NodeTransient node4=new NodeTransient("4");
        node1.next=node2;
        node2.pre=node1;
        node2.next=node3;
        node3.pre=node2;
        node3.next=node4;
        node4.pre=node3;

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(node1);
        out.close();

        byte[] bytes = bout.toByteArray();
        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(bytes));
        NodeTransient node=(NodeTransient)in.readObject();
        System.out.println(node);
    }

    public static void test1() throws IOException, ClassNotFoundException {
        Common c = new Common("common");
        A a = new A("a", c);
        B b = new B("b", c);


        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(a);
        out.writeObject(b);
        out.close();

        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(bout.toByteArray()));
        A a2 = (A) in.readObject();
        B b2 = (B) in.readObject();

        if (a2.getCommon() == b2.getCommon()) {
            System.out.println("reference the same object");
        } else {
            System.out.println("reference different objects");
        }
    }

    static class Common implements Serializable {
        String c;

        public Common(String c) {
            this.c = c;
        }
    }

    static public class Node implements Serializable {
        public String data;
        public Node pre=null;
        public Node next = null;

        public Node(String data) {
            this.data=data;
        }
    }

    static public class NodeTransient implements Serializable {
        public String data;
        public transient NodeTransient pre=null;
        public transient NodeTransient next = null;

        public NodeTransient(String data) {
            this.data=data;
        }
        private void writeObject(java.io.ObjectOutputStream s)
                throws java.io.IOException {
            s.defaultWriteObject();
            for (NodeTransient x = this; x != null; x = x.next)
                s.writeObject(x.data);
        }

    }

    static class A implements Serializable {
        String a;
        Common common;

        public A(String a, Common common) {
            this.a = a;
            this.common = common;
        }

        public Common getCommon() {
            return common;
        }
    }

    static class B implements Serializable {
        String b;
        Common common;

        public B(String b, Common common) {
            this.b = b;
            this.common = common;
        }

        public Common getCommon() {
            return common;
        }
    }

}
